package cn.jsu.oj.judgment.factory;


import cn.jsu.oj.judgment.handle.Handle;
import cn.jsu.oj.judgment.parsing.HandleType;
import cn.jsu.oj.judgment.utils.AnnotationPackageScanUtil;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 24987
 */
public class CmdHandleFactory {
    
    public static Map<Short, Handle> cmdHandleMap;
    static {
        cmdHandleMap = new ConcurrentHashMap<>();
        List<Class<?>> classes = AnnotationPackageScanUtil.scanPackage("cn", HandleType.class);
        for (Class<?> aClass : classes) {
            short type = aClass.getAnnotation(HandleType.class).type();
            try {
                cmdHandleMap.put(type, (Handle) aClass.newInstance());
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static Handle getHandle(short handleType)
    {
        return cmdHandleMap.get(handleType);
    }

}
